home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13432 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: delays: creating time delays with time.h (unix c)
  5. Date: Sat, 06 Apr 96 22:03:19 GMT
  6. Organization: none
  7. Message-ID: <828828199snz@genesis.demon.co.uk>
  8. References: <Pine.SUN.3.92.960404210632.28756A-100000@suntan>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <Pine.SUN.3.92.960404210632.28756A-100000@suntan>
  15.            cdiaz@eng.usf.edu "CS" writes:
  16.  
  17. >Hello! I'm trying to figure out if I can use the standard library (time.h)
  18. >to create a delay function where the program will pause for x amount of
  19. >time before continuing. 'x' I need it to be a value representing either
  20. >10th of a second or 100th of a second.
  21.  
  22. The only functions the standard library gives you to read time values are
  23. time() and clock(). You get no guarantees as to their granularity, however.
  24. In many systems time() has a granularity of 1 second so doesn't fit
  25. your criteria. clock() doesn't measure wall-clock time but rather CPU time
  26. used. On a system without multi-tasking this may amount to the same thing
  27. but isn't in general.
  28.  
  29. >Please, no references to using non-standard libraries: this code has to be
  30. >as platform independent as possible.
  31.  
  32. I think you'll just have to accept that there is no single portable function
  33. that meets your requirements. I suggest you create your own delay function
  34. and use conditional compilation to compile code in it that is appropriate to
  35. the system e.g. using clock, nap, usleep, select, poll etc. as appropriate.
  36. One thing you definitely don't want to do is busy-wait on a multi-tasking
  37. system.
  38.  
  39. >On that same note, suppose I have a variable getting the value from the
  40. >clock() function... how do I print the variable (of type clock_t or
  41. >time_t)? Thanks for your help!
  42.  
  43. A clock_t value doesn't mean much by itself, the only quantity that has
  44. meaning is the difference between two such values. You can print the difference
  45. in seconds out using something like:
  46.  
  47.      clock_t start, end;
  48.  
  49.      start = clock();
  50.  
  51.      ...
  52.  
  53.      end = clock();
  54.  
  55.      printf("%Lf\n", ((long double)end - start) / CLOCKS_PER_SEC);
  56.  
  57. For time_t there are various function to print it out formatted dates and
  58. times, e.g. ctime, gmtime, localtime, asctime, strftime. You can also use
  59. difftime to get the difference in seconds between 2 time_t values. The
  60. representation of time_t itself is unspecified so printing it out directly
  61. doesn't guarantee anything meaningful.
  62.  
  63. -- 
  64. -----------------------------------------
  65. Lawrence Kirby | fred@genesis.demon.co.uk
  66. Wilts, England | 70734.126@compuserve.com
  67. -----------------------------------------
  68.